package com.lkm.app_ui.m.Imple.display_second;

import android.os.Bundle;

import com.lkm.app_ui.m.display_second.DisplaySecondModule;
import com.lkm.app_ui.m.display_second.OnDisplaySecondShowChangeEvent;
import com.lkm.comlib.ui.BaseFragmentActivity;
import com.lkm.frame.otto.LinkBus;
import com.lkm.javatool.CollectionHelp;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;




public class DisplaySecondModuleImpe implements DisplaySecondModule {

    protected final List<MPresentationWarpFragment> runing=new ArrayList<MPresentationWarpFragment>();

    @Override
    public boolean show(BaseFragmentActivity activity, Fragment fragment) {

        MPresentationWarpFragment warpFragment=getInstance(this,fragment);
//        warpFragment.show(activity.getSupportFragmentManager(),warpFragment.getClass().getName());

        FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
        ft.add(warpFragment, warpFragment.getClass().getName());
        ft.commitAllowingStateLoss();

        runing.add(warpFragment);
        LinkBus.sendBroadcast(new OnDisplaySecondShowChangeEvent(true,fragment));

        return true;
    }

    @Override
    public boolean show(Fragment fragment) {
        BaseFragmentActivity activity= BaseFragmentActivity.getLastActivity();
        if(activity==null)return false;
        return show(activity,fragment);
    }

    @Override
    public boolean showSingleInstance(BaseFragmentActivity activity, Fragment fragment) {
        dismissAll(fragment.getClass());
        return show(  activity,   fragment);
    }

    private MPresentationWarpFragment lastBaseFragment=null;
    @Override
    public boolean showSingleInstanceInBase(BaseFragmentActivity activity, Fragment fragment) {
        dismissAll(fragment.getClass());
        MPresentationWarpFragment warpFragment=getInstance(this,fragment);
        warpFragment.show(activity.getSupportFragmentManager(),warpFragment.getClass().getName());
        if(lastBaseFragment!=null){

            lastBaseFragment.dismissAllowingStateLoss();
        }
        lastBaseFragment=warpFragment;
        LinkBus.sendBroadcast(new OnDisplaySecondShowChangeEvent(true,fragment));

        return true;
    }

    @Override
    public Fragment getBase() {
        return lastBaseFragment==null?null:lastBaseFragment.fragment;
    }

    @Override
    public boolean dismiss(Fragment fragment) {

        List<MPresentationWarpFragment> tempRuning=new ArrayList<MPresentationWarpFragment>(runing);
        for (MPresentationWarpFragment f: tempRuning) {
            if(f.fragment==fragment){
                f.dismissAllowingStateLoss();
                return true;
            }
        }

        return false;
    }

    @Override
    public boolean dismissLast(Class<? extends Fragment> fragmentClass) {
        List<MPresentationWarpFragment> tempRuning=new ArrayList<MPresentationWarpFragment>(runing);
        for (MPresentationWarpFragment f: tempRuning) {
            if(f.fragment.getClass()==fragmentClass){
                f.dismiss();
                return true;
            }
        }
        return false;
    }
    @Override
    public int dismissOther(Class<? extends Fragment> fragmentClass) {
        int count=0;
        List<MPresentationWarpFragment> tempRuning=new ArrayList<MPresentationWarpFragment>(runing);
        for (MPresentationWarpFragment f: tempRuning) {
            if(f.fragment.getClass()!=fragmentClass){
                f.dismiss();
                count++;
            }
        }
        return count;

    }

    @Override
    public int dismissAll(Class<? extends Fragment> fragmentClass) {
        int count=0;
        List<MPresentationWarpFragment> tempRuning=new ArrayList<MPresentationWarpFragment>(runing);
        for (MPresentationWarpFragment f: tempRuning) {
            if(f.fragment.getClass()==fragmentClass){
                f.dismiss();
                count++;
            }
        }
        return count;

    }

    @Override
    public int dismissExceptLast() {
        int count=0;
        List<MPresentationWarpFragment> tempRuning=new ArrayList<MPresentationWarpFragment>(runing);

        for (int i = 0; i < tempRuning.size()-1; i++) {
            MPresentationWarpFragment f=tempRuning.get(i);
            f.dismiss();
            count++;
        }
        return count;
    }
    @Override
    public boolean dismissLast() {
        if(!CollectionHelp.isEmpty(runing)){
            runing.get(runing.size()-1).dismiss();
            return true;
        }

        return false;
    }

    @Override
    public int dismissAll() {
        int count=runing.size();
        if(!CollectionHelp.isEmpty(runing)){
            clearAllUI();
            return count;
        }
        return count;
    }

    @Override
    public Fragment getLast() {
        if(!CollectionHelp.isEmpty(runing)){
            return runing.get(runing.size()-1).fragment;
        }
        return null;
    }

    private void clearAllUI(){
        List<MPresentationWarpFragment> tempRuning=new ArrayList<MPresentationWarpFragment>(runing);

        for (MPresentationWarpFragment f: tempRuning) {
            f.dismissAllowingStateLoss();
        }

    }



    private static MPresentationWarpFragment getInstance(DisplaySecondModuleImpe mDisplaySecondModuleImpe,final Fragment fragment) {
        MPresentationWarpFragment f = new MPresentationWarpFragment();
        Bundle data = new Bundle();
        f.setArguments(data);
        f.setFragment(mDisplaySecondModuleImpe,fragment);
        return f;
    }



    @Deprecated
    public static class MPresentationWarpFragment extends PresentationWarpFragment{
        private  Fragment fragment;

        private DisplaySecondModuleImpe mDisplaySecondModuleImpe;
        public void setFragment(DisplaySecondModuleImpe mDisplaySecondModuleImpe,Fragment fragment){
            this.mDisplaySecondModuleImpe=mDisplaySecondModuleImpe;
            this.fragment=fragment;
        }
        @Override
        protected Fragment getFragment() {
            return fragment;
        }

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);



        }

        @Override
        public void onDestroy() {
            if(mDisplaySecondModuleImpe.lastBaseFragment==this)mDisplaySecondModuleImpe.lastBaseFragment=null;

            mDisplaySecondModuleImpe.runing.remove(this);
            LinkBus.sendBroadcast(new OnDisplaySecondShowChangeEvent(false,fragment));
            super.onDestroy();
        }

    }


}
